package com.phpxiu.app.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.TextView;

import com.phpxiu.app.kkylive.R;
import com.phpxiu.app.model.list.Country;
import com.phpxiu.app.utils.KKYUtil;

import java.util.List;

/**
 * @file
 * @brief 国家或区域选择列表数据适配
 * @author 吴金洲
 * @note 国家或区域选择列表数据适配
 */
public class CountryPickListAdapter extends BaseAdapter<Country> {
    private AbsListView.LayoutParams paramsFirst,paramsOther;
    private int paddingTop=0;
    public CountryPickListAdapter(Context context, List<Country> data) {
        super(context, data);
        paddingTop= KKYUtil.dip2px(context, 15);
        paramsFirst=new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, KKYUtil.dip2px(context, 35));
        paramsOther=new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, KKYUtil.dip2px(context, 20));
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Country country=data.get(position);
        if(Country.GROUP==country.getListViewType()){
            if(convertView==null||convertView.getId()== R.id.country_list_item_view){
                convertView=mInflater.inflate(R.layout.country_list_group_view,null);
            }
            if("A".equals(country.getGroup())){
                convertView.setPadding(convertView.getPaddingLeft(),paddingTop,0,0);
                convertView.setLayoutParams(paramsFirst);
            }else{
                convertView.setPadding(convertView.getPaddingLeft(),0,0,0);
                convertView.setLayoutParams(paramsOther);
            }
            TextView groupView=(TextView)convertView;
            groupView.setText(country.getGroup());
        }
        if(Country.ITEM==country.getListViewType()){
            if(convertView==null||convertView.getId()==R.id.country_list_group_view){
                convertView=mInflater.inflate(R.layout.country_list_item_view,null);
            }
            TextView regionView=(TextView)convertView.findViewById(R.id.region_view);
            regionView.setText(country.getName());
            TextView codeView=(TextView)convertView.findViewById(R.id.code_view);
            codeView.setText("+"+country.getCode());
            convertView.setTag(country);
        }
        return convertView;
    }
}
